Skip to content

Make repair data always write --output, and read inlined references - #159

Merged
caufieldjh merged 3 commits into
mainfrom
ccr-65a5480d-pb91sk
Oct 31, 2026
Merged

caufieldjh merged 3 commits into
mainfrom
ccr-65a5480d-pb91sk

Conversation

@caufieldjh

Copy link
Copy Markdown
Contributor

Fixes #143. Fixes #138.

Both are bugs in repair data (cli/repair.py).

#143: --no-dry-run -o OUT didn't write OUT when there was nothing to fix

OUT was written only inside if not dry_run and report.auto_fixed_count > 0. That left two paths that exited 0 with no OUT: an input that needed no fixes, and the early exit when no evidence items were found. A pipeline that reads OUT next would then fail, or read a stale file from an earlier run.

  • OUT is now always written outside a dry run. That's the repaired data when a fix was applied. Otherwise it's a byte-for-byte copy of the input (_copy_unchanged), so ruamel round-tripping can't change anything. The message says Wrote OUT (no changes).
  • The .bak backup is made only when the input itself is overwritten. With -o elsewhere, the input is never touched, so a backup of it served no purpose.
  • -o naming the input, with nothing to fix, leaves the file alone. Copying a file onto itself would otherwise be an error.
  • No -o and nothing applied: the input isn't rewritten. Before, if the report counted auto-fixes but none could be put back into the data, the input was rewritten anyway and backed up for nothing. Now the command reports ! N auto-fix(es) could not be placed back in the data and leaves the file untouched.

The docs (reference/cli.md, how-to/repair-validation-errors.md) now describe this.

#138: repair data crashed on an inlined reference object

_extract_evidence_items passed reference: {id: ..., title: ...} on whole as the reference id. The repair then crashed with AttributeError: 'CommentedMap' object has no attribute 'strip'. The repo's own tests/data/test_data_invalid.yaml uses that shape.

  • Shared helper. The plugin's _extract_reference_id is now a module-level extract_reference_id in plugins/reference_validation_plugin.py. It sits outside the if _LINKML_AVAILABLE block, so repair data doesn't need LinkML installed. The plugin method delegates to it, and _extract_evidence_items uses it, so the two read references the same way. That's what the issue suggested.
  • A reference that names no single id is skipped, with a warning naming the item. That covers a list of references, an object with no id, and a non-text id. All of these used to crash the same way. A repair needs the one reference the quote came from, so picking one from a list could repair against the wrong paper.

With this, the issue's reproduction (repair data tests/data/test_data_invalid.yaml -s tests/data/test_schema.yaml) prints a repair report instead of a traceback.

Not in this PR

#143 also notes that --schema and --target-class are accepted but unused. The issue thread suggested splitting that out, so this PR leaves it alone.

Tests

tests/test_repair_data_output.py was written first, and 10 of its tests failed on main. They're real CLI runs against the fixture cache, so nothing is fetched.

Full suite: 2302 passed. Doctests, mypy and ruff are clean.


Generated by Claude Code

With --no-dry-run --output OUT, OUT was written only when something was
auto-fixed. When nothing needed fixing, or no evidence items were found,
the command exited 0 with no OUT, so a pipeline reading it next failed
or read a stale file (#143). OUT is now always written outside a dry
run: the repaired data, or a byte-for-byte copy of the input when
nothing changed. A .bak backup is made only when the input itself is
overwritten, and an output naming the input with nothing to fix is left
alone.

An evidence item whose reference is an inlined object passed the whole
mapping on as the reference id, and repair data crashed with
AttributeError: 'CommentedMap' object has no attribute 'strip' (#138).
The plugin's id reading is now a module-level extract_reference_id,
which the plugin method and _extract_evidence_items both use. A
reference that names no single id (a list, or an object without a text
id) is skipped with a warning naming the item, rather than crashing.

Fixes #143
Fixes #138

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qm4PkMsSN3FVpzvBdZYahC
@claude

claude Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Claude encountered an error after 0s —— View job


I'll analyze this and get back to you.

Copy link
Copy Markdown
Contributor Author

claude-review is red, but not because of this PR. This is the same failure as on #158. In the log, the Claude run ended after 274 ms with is_error: true, total_cost_usd: 0 and an empty modelUsage, so the review never started.

I can't re-run the job from here (403 on #158). A maintainer can re-run it from the Actions page, and the next push will also re-run it. #158 passed its review on a manual re-run.

The test jobs are still running. I'll keep watching.


Generated by Claude Code

@claude

claude Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @caufieldjh's task in 59s —— View job


PR Review: Make repair data always write --output, and read inlined references

  • Read the diff and the new tests
  • Check _extract_evidence_items, _apply_repairs_to_data and the logging setup around the change
  • Post review

Summary: This is a clean, well-scoped fix for both issues. I found no blocking problems. The notes below are small follow-ups. (I couldn't run pytest because the sandbox needs approval for uv run, so I went by reading the code.)

What works well

  • One way to read references. extract_reference_id now lives at module level, outside if _LINKML_AVAILABLE, and the plugin method delegates to it. repair data and validation now read references the same way, and the doctests cover the edge cases ({"id": 123}, lists).
  • _copy_unchanged copies the file instead of re-dumping it. Re-dumping with ruamel could reformat the file even when nothing changed, so a byte-for-byte copy is the right call.
  • The backup rule makes sense. .bak is made only when _same_file(output_path, data_file), i.e. when the input is actually overwritten. -o input.yaml with nothing to fix is a no-op, where it could otherwise have failed copying the file onto itself.
  • No pointless rewrite. Before, if auto_fixed_count > 0 but _apply_repairs_to_data placed nothing, the input was rewritten anyway and backed up for nothing. Now it prints a ! message and leaves the file alone.
  • Tests run the real CLI against the fixture cache. They aren't mocks, which matches the guidance in CLAUDE.md.

Suggestions (non-blocking)

  1. The "could not be placed back" branch has no test. cli/repair.py: the else branch where report.auto_fixed_count > 0 but _apply_repairs_to_data returns False is untested. It's the one new user-facing message with no coverage. You could reach it by handing _apply_repairs_to_data a report whose result.path matches no excerpt key. Even a unit-level test would pin down the "input untouched, -o gets a copy" behaviour there.

  2. Partial placement isn't reported. If 3 fixes are counted but only 2 land, the output still says Applied 3 auto-fix(es). _apply_repairs_to_data returns a bool, so it can't tell you. Returning a count would make both messages accurate. This was already true before the PR, so it's fine to leave for later.

  3. Where the skip warning goes. The warning for an unrepairable reference uses logger.warning. setup_logging only calls basicConfig with --verbose. Without it, the message still reaches stderr through Python's last-resort handler, but bare, with no level or logger name. That's acceptable. Since everything else in this command uses typer.echo, consider typer.echo(..., err=True) so the warning looks consistent. Either way the test relies on caplog, so it would need adjusting.

  4. Edge case: a reference object with no id next to a sibling reference_id. With {reference: {title: X}, reference_id: "PMID:1", supporting_text: ...}, d.get("reference") or d.get("reference_id") picks the dict, which has no id, so the item is skipped even though a usable reference_id sits beside it. The shape is unlikely, so I'm only mentioning it.

  5. Small point on _same_file. Path.resolve() follows symlinks, so a symlinked -o that points at the input counts as the same file. That's the right behaviour, and a short note in the docstring would make it explicit.

Other checks

  • Security: nothing to flag. Output paths come from the user's own CLI arguments, and shutil.copyfile doesn't follow anything unexpected.
  • Performance: _extract_evidence_items does one extra function call per dict, which costs nothing measurable.
  • Docs: the --output wording is accurate. "The input itself is not touched, and no backup is made" could be misread as covering the default no--o case too. Starting that sentence with "When --output is given, …" would remove the ambiguity.
  • Scope: leaving the unused --schema and --target-class options for a separate PR is reasonable.

Overall: LGTM once you've considered the test for suggestion 1.

From the PR review:

- _extract_evidence_items reads reference, then reference_id, each in
  turn, so a reference object with no id no longer hides a usable
  reference_id beside it.
- A test covers fixes that cannot be placed back: the input is left
  alone and --output still gets an unchanged copy.
- The --output docs say plainly that the input is untouched only when
  --output is given, and _same_file notes that symlinks are followed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qm4PkMsSN3FVpzvBdZYahC

Copy link
Copy Markdown
Contributor Author

Addressed the review in c3545d3:

  1. The "could not be placed back" branch had no test: a test now covers it. Extraction and write-back use the same paths, so no real data reaches this branch, and the test stubs _apply_repairs_to_data to report that nothing was placed. It checks that the input is left alone, that -o gets a byte-identical copy, and that no .bak is made.
  2. Partial placement: left as is. It predates this PR, and fixing it means _apply_repairs_to_data returning a count, which can be a follow-up.
  3. The skip warning uses logger.warning: kept. It reaches stderr without --verbose, and with --verbose it is formatted like every other warning the validator logs.
  4. Reference object with no id next to a sibling reference_id: fixed. reference and reference_id are now each read in turn, and there is a new test for this case.
  5. _same_file: the docstring now says symlinks are followed.

Docs: the --output text now starts "When given with --no-dry-run, …", so the untouched-input part clearly applies only when -o is given.

Full suite: 2304 passed. Doctests, mypy and ruff are clean.


Generated by Claude Code

@claude

claude Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @caufieldjh's task in 42s —— View job


PR Review (re-review after c3545d3)

  • Read the new commit and the full diff
  • Run the tests. Not done: uv run pytest needs approval in this sandbox, so this review is from reading the code. CI covers it.
  • Post review

Summary: The new commit deals with the earlier review's points 1, 4 and 5 and with the docs wording. I found nothing blocking. LGTM.

What c3545d3 changes

  • Sibling reference_id fallback (cli/repair.py:417-419). extract_reference_id(d.get("reference")) or extract_reference_id(d.get("reference_id")) reads each key in turn. {reference: {title: T}, reference_id: "PMID:3"} now resolves to PMID:3, and test_extract_falls_back_to_a_sibling_reference_id pins that down. The skip warning still uses ref_value (the first truthy key), so it fires only when neither key gives an id. That's correct.
  • The unplaced-fix branch now has a test (test_fixes_that_cannot_be_placed_leave_the_input_alone). It checks the ! message, that the input is untouched, that -o gets a byte-identical copy, and that no .bak is made.
  • Docs. Both --output descriptions now start with "When given with --no-dry-run", which fixes the ambiguity I raised before.
  • _same_file docstring now says that symlinks are followed.

Minor notes (non-blocking)

  1. The new test stubs _apply_repairs_to_data with monkeypatch. CLAUDE.md says to avoid mock tests unless asked. The docstring explains why: extraction and write-back share their paths, so real data can't reach this branch. The rest of the test runs the real CLI, so I think the stub is justified. The flip side is that the else branch is defensive code that real input can't currently reach. If you'd rather not keep a stub, you could drop the test and add a comment that the branch is a safeguard.
  2. Repair and validation now differ a little. The plugin reads only the reference slot, iterates lists (reference_validation_plugin.py:405-407), and doesn't fall back to a sibling reference_id. repair data now does that fallback and skips lists. Both choices are reasonable for repair: it needs exactly one source, and the fallback is lenient. But "reads references the same way" is now only mostly true. You could say so in the _extract_evidence_items comment.
  3. Still open from before: the "Applied N auto-fix(es)" count assumes every counted fix landed (point 2), and the skip warning goes through logger.warning rather than typer.echo(err=True) (point 3). Both are fine to leave for a follow-up.

Other checks

  • Security / performance: nothing new. The extra extract_reference_id call costs nothing measurable.
  • Code quality: the change is small and well commented, and there's no new try/except.

Copy link
Copy Markdown
Contributor Author

Notes from the re-review of c3545d3:

  • 2 (repair vs. validation): fixed in 4c70efd. The comment in _extract_evidence_items now says where repair departs from validation. It skips a list of references, because a repair needs exactly one source, and it falls back to a sibling reference_id. The id itself is still read with extract_reference_id.
  • 1 (the stub): kept. The test drives the real CLI and stubs only the write-back, which real data can't currently make fail. Without the stub, the user-facing message and the no-rewrite behaviour in that branch would have no test at all.
  • 3: both points stay as follow-ups, as noted above.

Generated by Claude Code

@claude

claude Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @caufieldjh's task in 34s —— View job


PR Review (re-review after 4c70efd)

  • Read the new commit and the full source diff against origin/main
  • Run the tests. Not done: uv run pytest needs approval in this sandbox, so this review comes from reading the code. CI covers the tests.
  • Post review

Summary: 4c70efd only changes a comment. It answers note 2 from my last review: the comment in _extract_evidence_items (cli/repair.py:412-418) now says where repair data reads references differently from validation. It skips lists, because a repair needs exactly one source, and it tries reference and then reference_id, so a usable sibling id is still found. The rewritten comment is accurate, and moving it above both lines makes the block easier to read. No code changed, so nothing new can break. LGTM.

One small wording point (non-blocking)

  • The extract_reference_id docstring (plugins/reference_validation_plugin.py:46-47) still says code outside the plugin "reads references the same way". That's true of the helper itself, but repair data now wraps it in its own rules. You could change it to something like "…reads a single reference value the same way", so it doesn't contradict the new comment in repair.py.

Still open, fine as follow-ups

  1. Applied N auto-fix(es) assumes every counted fix landed. To fix this, _apply_repairs_to_data would need to return a count instead of a bool.
  2. The skip warning uses logger.warning, while the rest of the command prints with typer.echo. The earlier reply explained why, and that's reasonable.

Other checks

  • Correctness: the write and backup logic is unchanged since my last review and still matches the PR description. With nothing to fix, -o gets a byte-identical copy. .bak is made only when the input is overwritten. A dry run writes nothing.
  • Security / performance: nothing new.
  • Tests: unchanged by this commit. The coverage from c3545d3 still applies.

Copy link
Copy Markdown
Contributor Author

On the extract_reference_id docstring wording: leaving it as is. It describes what the helper does with one reference value, and both callers do read a value that way. Where repair data adds rules on top (skipping lists, falling back to a sibling reference_id), the comment at its call site now says so. The two remaining follow-ups are unchanged.


Generated by Claude Code

@caufieldjh
caufieldjh merged commit 7fcb8a8 into main Oct 31, 2026
5 checks passed
@caufieldjh
caufieldjh deleted the ccr-65a5480d-pb91sk branch October 31, 2026 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants